import java.awt.*;
import java.awt.event.*;
import java.sql.*;

@SuppressWarnings("serial")
public class Diary extends Frame implements ActionListener, ItemListener{
	public static final int  NONE   = 0;
	public static final int  ADD    = 1;
	public static final int  DELETE = 2;
	public static final int  SEARCH = 3;
	public static final int  TOTAL  = 4;
	
	public static TextArea  display;
	public static TextField  HWname, Chapter;
	public static Choice cs_name, Smes, Year, Day, DM, DD, DT, SF, level;
	public static Button     add, delete, search, total, etc, pro, cancel;
	public static Connection con;
	public static Statement  stat;
	public static int        cmd, ytemp, smtemp;
	public static ResultSet rs = null;
	public static TextField        tf_temp;
	public static Choice        ch_temp;
	
	public Diary() {
		super("Diary");
		init();
		setLayout(new BorderLayout());
		display = new TextArea();
		display.setEditable(false);

		Panel left = new Panel();
		left.setLayout(new GridLayout(6,1));
		
		Panel year = new Panel();
		year.add(new Label("Year"));
		year.add(Year = new Choice());
		Year.add("2009");
		Year.add("2010");
		Year.add("2011");
		Year.add("2012");
		
		String year_string = Year.getSelectedItem();
		int year_int = Integer.parseInt(year_string);
		Year.addItemListener(this);
		ytemp = year_int;
		
		year.add(new Label("Course"));
		year.add(cs_name = new Choice());
		try{
			stat = con.createStatement();
			rs = stat.executeQuery("select * from Courses where Year="+year_int+"order by Semester" );
			while(rs.next()) {
				String cs_string = rs.getString(1);
				cs_name.add(cs_string);
			}
		}catch(Exception ex){
			System.out.println("Error...");
		}
		
		Panel HW = new Panel();
		HW.add(new Label("HWname"));
		HW.add(HWname = new TextField(20));
		
		Panel Smonth = new Panel();
		Smonth.add(new Label("Realese Month"));
		Smonth.add(Smes = new Choice());
		for(int i = 1 ; i < 13 ; i++) {
			Smes.add(""+i+"");
		}
		String sm_string = Smes.getSelectedItem();
		int sm_int = Integer.parseInt(sm_string);
		Smes.addItemListener(this);
		smtemp = sm_int;
		
		Smonth.add(new Label("Realese Day"));
		Smonth.add(Day = new Choice());
		for(int i = 1 ; i < 32 ; i++) {
			Day.add(""+i+"");
		}
		
		Panel Dmonth = new Panel();
		Dmonth.add(new Label("Deadline Month"));
		Dmonth.add(DM = new Choice());
		for(int i = 1 ; i < 13 ; i++) {
			DM.add(""+i+"");
		}
		String dm_string = Smes.getSelectedItem();
		int dm_int = Integer.parseInt(dm_string);
		DM.addItemListener(this);
		smtemp = dm_int;
		
		Dmonth.add(new Label("Deadline Day"));
		Dmonth.add(DD = new Choice());
		for(int i = 1 ; i < 32 ; i++) {
			DD.add(""+i+"");
		}
		
		Panel DTime = new Panel();
		DTime.add(new Label("Deadline Time"));
		DTime.add(DT = new Choice());
		for(int i = 0 ; i < 24 ; i++) {
			DT.add(""+i+"");
		}
		
		Panel Pro = new Panel();
		Pro.add(new Label("Chapter"));
		Pro.add(Chapter = new TextField(15));
		
		Pro.add(new Label("Year"));
		Pro.add(level = new Choice());
		level.add("1");
		level.add("2");
		level.add("3");
		
		left.add(year);
		left.add(HW);
		left.add(Smonth);
		left.add(Dmonth);
		left.add(DTime);
		left.add(Pro);
		
		
		Panel bottom = new Panel();
		bottom.add(add = new Button("Add"));
		add.addActionListener(this);
		bottom.add(delete = new Button("Delete"));
		delete.addActionListener(this);
		bottom.add(search = new Button("Collision"));
		search.addActionListener(this);
		bottom.add(total = new Button("All"));
		total.addActionListener(this);
		bottom.add(etc = new Button("Etc."));
		etc.addActionListener(this);
		//bottom.add(pro = new Button("Progress"));
		//pro.addActionListener(this);
		bottom.add(cancel = new Button("Cancle"));
		cancel.addActionListener(this);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				destroy();
				setVisible(false);
				dispose();
				//System.exit(0);
			}
		});
		add("Center", display);
		add("West", left);
		add("South", bottom);
		cmd = NONE;
		initialize();
	}

	private void init() {
		try{
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		}catch(ClassNotFoundException ce){
			System.out.println(ce);
		}
		try{
			con =DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Seoyoung", "sa", "pw");
			System.out.println("You access DBMS because of Diary");
			stat = con.createStatement();
		}catch(SQLException se){
			System.out.println(se);
		}finally{
			//try{
			//}catch(SQLException se){}
		}
		
	}


	public void initialize() {
		cs_name.setEnabled(false);
		HWname.setEditable(false);
		Chapter.setEditable(false);
		Smes.setEnabled(false);
		Year.setEnabled(false);
		Day.setEnabled(false);
		DM.setEnabled(false);
		DD.setEnabled(false);
		DT.setEnabled(false);
		level.setEnabled(false);
		HWname.setText("1st, Fit for purpose and press the button.");
		Chapter.setText("1st, Fit for purpose and press the button.");
	}

	private void destroy() {
		try {
			if(stat != null) {
				stat.close();
			}
			if(con != null) {
				con.close();
			}
			if(rs != null){
				rs.close();
			}
		} catch(Exception ex) { }
	}

	public static void setEditable(int n) {
		switch(n) {
		case ADD:
			cs_name.setEnabled(true);
			HWname.setEditable(true);
			Smes.setEnabled(true);
			Year.setEnabled(true);
			Day.setEnabled(true);
			DM.setEnabled(true);
			DD.setEnabled(true);
			DT.setEnabled(true);
			Chapter.setEditable(true);
			level.setEnabled(true);
			break;
			
		case DELETE:
			HWname.setEditable(true);
			break;
			
		case SEARCH:
			
			
			break;
			
		case NONE:
			cs_name.setEnabled(false);
			HWname.setEditable(false);
			Smes.setEnabled(false);
			Year.setEnabled(false);
			break;
			
		case TOTAL:
			cs_name.setEnabled(false);
			HWname.setEditable(false);
		}	
	}

	public static void setEnable(int n) {
		add.setEnabled(false);
		delete.setEnabled(false);
		search.setEnabled(false);
		total.setEnabled(false);
		etc.setEnabled(false);
		//pro.setEnabled(false);
		switch(n) {
			case ADD:
				add.setEnabled(true);
				setEditable(ADD);
				cmd = ADD;
				break;
			case DELETE:
				delete.setEnabled(true);
				setEditable(DELETE);
				cmd = DELETE;
				break;
			case SEARCH:
				search.setEnabled(true);
				setEditable(SEARCH);
				cmd = SEARCH;
				break;
			case TOTAL:
				total.setEnabled(true);
				cmd = TOTAL;
				break;
			case NONE:
				add.setEnabled(true);
				delete.setEnabled(true);
				search.setEnabled(true);
				total.setEnabled(true);
				etc.setEnabled(true);
				//pro.setEnabled(true);
		}
	}

	public void clear() {
		
		
	}

	public void actionPerformed(ActionEvent e) {
		Component c = (Component) e.getSource();
		try {
			if(c == add) {
				if(cmd != ADD){
					setEnable(ADD);
					System.out.println("You are going to adding Someting...");
				}
				else {
					
					String cs_string = cs_name.getSelectedItem();
					String hw_string = HWname.getText().trim();
					String chap = Chapter.getText().trim();
					String sm_string = Smes.getSelectedItem();
					//String year_string = Year.getSelectedItem();
					String Day_s = Day.getSelectedItem();
					String DM_s = DM.getSelectedItem();
					String DD_s = DD.getSelectedItem();
					String DT_s = DT.getSelectedItem();
					String le_s = level.getSelectedItem();
					
					int sm_int = Integer.parseInt(sm_string);
				//	int year_int = Integer.parseInt(year_string);
					int Day_int = Integer.parseInt(Day_s);
					int DM_int = Integer.parseInt(DM_s);
					int DD_int = Integer.parseInt(DD_s);
					int DT_int = Integer.parseInt(DT_s);
					int le_int = Integer.parseInt(le_s);
					
					if(cs_string == null || hw_string == null || cs_string.length() == 0 || hw_string.length() == 0)
							return ;
					String sql = "insert into HWS values('" + cs_string + "', '" + cs_string+"-"
					+ hw_string + "'," + sm_int +"," + Day_int +","+ DM_int +","+ DD_int +","+ DT_int +")";
					stat.executeUpdate(sql);
					String sql2 = "insert into Progress values('"+ cs_string+"-"+ hw_string + "','" + cs_string+"-"
					+ chap + "'," + le_int+")";
					stat.executeUpdate(sql2);
					System.out.println("Successful insertion!");
					setEnable(NONE);
					clear();
					cmd = NONE;
					initialize();
					

				}
			} else if(c == delete) {
				if(cmd != DELETE){
					setEnable(DELETE);
					System.out.println("You are going to deleting Someting...");
				}else {
					String hw_string = HWname.getText().trim();
					if(hw_string == null || hw_string.length() == 0)
						return;
					String sql = "delete from Progress where Course='"+hw_string + "'";
					stat.executeUpdate(sql);
					sql = "delete from HWS where HName='"+hw_string + "'";
					stat.executeUpdate(sql);
					//sql = "delete from Progress where Course='"+ rs+ "'";
					
					System.out.println(sql);
					//stat.executeUpdate(sql);
					System.out.println("Successful Deletaion!");
					setEnable(NONE);
					clear();
					cmd = NONE;
					initialize();
					
				}
			} else if(c == search) {
				rs = stat.executeQuery("SELECT E.Ename, E.Time, H.HName, H.DTime, H.DMonth, H.Dday , C.Year "
										+ "From Event E, HWS H, Courses C " + 
										"WHERE "+ 
											"E.Month = H.DMonth AND "  
											+"E.Day = H.Dday AND " 
											+"H.Course =C.Course;");
				display.setText("================================================================" +
						"==============================================================================================" +"\n");
				display.append("\t\tEvent \t\t Time \t\t Homework \t\t Deadline(Time) \t\t Date\n");
				display.append("===============================================================================" +
						"===============================================================================" +"\n");
				while(rs.next()) {
					String Ename = rs.getString(1);
					String Hname = rs.getString(3);
					int ET = rs.getInt(2);
					int DT = rs.getInt(4);
					int dm = rs.getInt(5);
					int dd =  rs.getInt(6);
					int dy = rs.getInt(7);
					
					display.append("\t"+Ename +"\t  "+ET + "\t\t" +Hname +"\t\t\t" +DT+""+"\t\t"+ dy+ "."+ dm +"."+dd+ "\n");
					display.append("---------------------------------------------------------------------" +
							"-----------------------------------------------------------------------------------------\n");
					cmd = NONE;
				}
			} else if(c == total) {
				rs = stat.executeQuery("select C.Course, H.HName, P.Chapter, " +
						"P.Satisfaction, H.SMonth, H.Sday, H.DMonth, H.Dday, H.DTime, C.Professor, " +
						"C.Semester, C.Year from Courses C, HWS H, Progress P where C.Course = H.Course AND P.Course = H.HName " +
						"order by C.Year, H.DMonth, H.Dday, H.DTime;");
				display.setText("=====================================================================================" +
						"==============================================================================================" +"\n");
				display.append("\tCourse \t\t Homework \t\t Chapter(Level) \t\t Release \t\t Deadline \t\t Prof. \t\t Semester \t Year\n");
				display.append("=======================================================================================" +
						"============================================================================================" +"\n");
				while(rs.next()) {
					String cs_name = rs.getString(1);
					String HW_name = rs.getString(2);
					String ch_name = rs.getString(3);
					int sa = rs.getInt(4);
					int rlm = rs.getInt(5);
					int rd = rs.getInt(6);
					int dm = rs.getInt(7);
					int dt =  rs.getInt(8);
					int dti = rs.getInt(9);
					String prof_nm = rs.getString(10);
					String sm = rs.getString(11);
					int year = rs.getInt(12);
					
					display.append("\t"+ cs_name + " \t\t" + HW_name + " \t\t" + ch_name+"("+sa+")"+ "\t\t" + rlm + "." + rd +"\t\t"+ dm +"."+ dt + "   "
							+ dti +"h \t\t" + prof_nm + "\t"+ sm + "\t\t" + year +"\n");
					display.append("----------------------------------------------------------------------------------------" +
							"-------------------------------------------------------------------------------------------\n");
					cmd = NONE;
				}
				
				
			} else if(c == etc){
				Etc et = new Etc();
				et.setSize(1000, 500);
				et.setVisible(true);
			}
				else if(c == cancel) {
				setEnable(NONE);
				initialize();
				cmd = NONE;
			}
		} catch(Exception ex) { }

		return ;
	}
	
	public void itemStateChanged(ItemEvent e){
		Component c = (Component) e.getSource();
		if(c == Year){
			try{
				cs_name.removeAll();
				String year_string = Year.getSelectedItem();
				ytemp = Integer.parseInt(year_string);
				stat = con.createStatement();
				rs = stat.executeQuery("select * from Courses where Year="+ytemp+"order by Semester" );
				while(rs.next()) {
					String cs_string = rs.getString(1);
					cs_name.add(cs_string);
				}
			}catch(Exception ex){
				System.out.println("Error...");
			}
		}else if(c == Smes){
			Day.removeAll();
			String year_string = Year.getSelectedItem();
			ytemp = Integer.parseInt(year_string);
			String sm_string = Smes.getSelectedItem();
			smtemp = Integer.parseInt(sm_string);
			if(smtemp == 1 || smtemp == 3 || smtemp == 5 || smtemp == 7 || smtemp == 8 || smtemp == 10 || smtemp == 12){
				for(int i = 1 ; i < 32 ; i++) {
					Day.add(""+i+"");
				}
				
			}else if(smtemp == 2){
				if(ytemp == 2012)
					for(int i = 1 ; i < 30 ; i++) {
						Day.add(""+i+"");
					}
				else
					for(int i = 1 ; i < 29 ; i++) {
						Day.add(""+i+"");
					}
			}else{
				for(int i = 1 ; i < 31 ; i++) {
					Day.add(""+i+"");
				}
			}
			
		}else if(c == DM){
			DD.removeAll();
			String year_string = Year.getSelectedItem();
			ytemp = Integer.parseInt(year_string);
			String sm_string = DM.getSelectedItem();
			smtemp = Integer.parseInt(sm_string);
			if(smtemp == 1 || smtemp == 3 || smtemp == 5 || smtemp == 7 || smtemp == 8 || smtemp == 10 || smtemp == 12){
				for(int i = 1 ; i < 32 ; i++) {
					DD.add(""+i+"");
				}
			}else if(smtemp == 2){
				if(ytemp == 2012)
					for(int i = 1 ; i < 30 ; i++) {
						DD.add(""+i+"");
					}
				
				else
					for(int i = 1 ; i < 29 ; i++) {
						DD.add(""+i+"");
					}
				
			}else{
				for(int i = 1 ; i < 31 ; i++) {
					DD.add(""+i+"");
				}
			}
		}
		return;
	}
	
}
